<template>
    <el-dialog :title="title" :visible.sync="open" width="300px" append-to-body>
        <div>
            <el-row>
                <el-tree
                    ref="tree"
                    :data="data"
                    show-checkbox
                    check-strictly
                    @check-change="handleCheckChange"
                    node-key="id"
                    :props="{
                        children: 'childList',
                        label: 'name',
                    }"></el-tree>
            </el-row>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="onSubmit">确认</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { getDeptList } from "@/api/system/dept";
import dialogMixin from "@/mixins/dialogMixin";

export default {
    name: "DeptPicker",
    mixins: [dialogMixin],
    props: {
        muilt: {
            type: Boolean,
            default: true,
        },
    },
    data() {
        return {
            title: "选择部门",
            data: [],
            selectedNode: null,
            selectedNodes: [],
        };
    },
    created() {
        this.getDept();
    },
    mounted() {},
    computed: {},
    methods: {
        show() {
            this.open = true;
        },
        getDept() {
            getDeptList().then(res => {
                this.data = res.data;
            });
        },
        handleCheckChange(data, checked) {
            if (this.muilt) {
                this.selectedNodes = this.$refs.tree.getCheckedNodes();
            } else {
                if (checked) {
                    this.$refs.tree.setCheckedNodes([data]);
                }
                this.$nextTick(() => {
                    this.selectedNode = this.$refs.tree.getCheckedNodes()[0];
                });
            }
        },
        onSubmit() {
            if (this.muilt) {
                this.$emit("handleOk", this.selectedNodes);
            } else {
                this.$emit("handleOk", this.selectedNode);
            }
            this.close();
        },
    },
};
</script>
<style scoped>
.title {
    line-height: 30px;
    margin-bottom: 10px;
    font-weight: bold;
    color: #409eff;
    margin: unset !important;
}

.name-card {
    display: flex;
    align-items: center;
}

.name-card:hover {
    cursor: pointer;
}

.el-card {
    flex: 1;
    margin-left: 15px;
}

.card-form {
    flex: 1;
    text-align: right;
    display: flex;
    align-items: center;
}

.el-card /deep/ .el-card__header > div {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.el-card /deep/ .el-card__header > div > span {
    flex: 1;
}

.card-form .el-radio:first-of-type {
    margin-left: 30px;
}

.address-card /deep/ .el-card__body {
    display: flex;
    flex-flow: row;
    padding: 10px;
}

.avatar {
    width: 40px;
    height: 40px;
}

.avatar img {
    width: inherit;
    height: inherit;
}

.info {
    flex: 1;
    display: flex;
    flex-flow: column;
    margin-left: 10px;
}

.name {
    flex: 1;
    font-size: 18px;
    border-bottom: 1px solid white;
    padding-bottom: 5px;
}

.name span {
    font-size: 13px;
    margin-left: 10px;
    color: grey;
}

.contact {
    height: 26px;
    line-height: 26px;
    font-size: 12px;
    display: flex;
    justify-content: space-between;
    color: white;
}
</style>
